package com.cell.controller;

import com.cell.common.Result;
import com.cell.common.States;
import com.cell.pojo.DTO.user.location.UserUpdateLocationDTO;
import com.cell.service.user.location.IUserLocationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author cloudJiang
 * @date 2022/6/24 14:11
 */
@RestController
@RequestMapping("/userLocation")
@Api(tags = "用户位置获取以及修改相关接口")
public class UserLocationController {
    @Autowired
    IUserLocationService locationService;

    /**
     * 修改用户地址（普通用户）
     *
     * @return
     */
    @ApiOperation(value = "用户地址修改", notes = "点击修改地址后应先调用/preUpdateLocation方法来获取当前地址，并在这个基础上来修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "locationId", value = "用户位置信息表id", type = "Long", required = true),
            @ApiImplicitParam(name = "province", value = "省份", required = true),
            @ApiImplicitParam(name = "city", value = "城市名称", required = true),
            @ApiImplicitParam(name = "district", value = "所在城市区域名称", required = true),
            @ApiImplicitParam(name = "community", value = "所在社区名称", required = true),
            @ApiImplicitParam(name = "locationDetails", value = "详细地址，XX栋XX楼XX号", required = true),
            @ApiImplicitParam(name = "userUpdateLocationDTO", value = "以上属性被封装到这个DTO里")
    })
    @PostMapping("/updateLocation")
    Result updateLocation(@Validated @RequestBody UserUpdateLocationDTO userUpdateLocationDTO) {
        locationService.updateLocation(userUpdateLocationDTO);
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 修改用户地址前先获取目前的地址并返回（普通用户）
     *
     * @return
     */
    @ApiOperation(value = "获取用户位置信息", notes = "获取用户位置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "locationId", value = "用户位置信息表id", type = "Long", required = true),
    })
    @GetMapping("/preUpdateLocation")
    Result preUpdateLocation(@RequestParam("locationId") Long locationId) {
        return new Result(locationService.getLocation(locationId),
                States.OK.getStates(), States.OK.getDescription());
    }
}
